package com.bprogram.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bprogram.pojo.Upload;
import com.bprogram.pojo.User;
import com.bprogram.pojo.Video;
import com.bprogram.service.UploadService;

import com.bprogram.utils.RedisUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cai
 * @since 2023-11-21
 */
@RestController
@RequestMapping("/upload")
public class UploadController {


    @Autowired
    private UploadService uploadService;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private VideoFeign videoFeign;
    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/uploadOnServer")
    public String upload(@RequestParam MultipartFile file, @RequestParam String type) {
        //上传文件到七牛云服务器
        try {
            return uploadService.getFileAskingPath(uploadService.uploadFile(file, type));
        } catch (RuntimeException e) {
            return e.getMessage();
        }
    }

    @DeleteMapping("/deleteInServer")
    public String deleteInServer(@RequestParam String key) {
        try {
            return uploadService.deleteFile(key) ? "文件已删除" : "删除失败";
        } catch (RuntimeException e) {
            return e.getMessage();
        }
    }

    @GetMapping("/deleteInDB")
    public boolean deleteInDB(long id) {
        return uploadService.removeById(id);
    }

    @GetMapping("/getServerInfo")
    public FileInfo getFileInfo(String key) {
        return uploadService.checkFile(key);
    }

    /**
     * 每天凌晨两点执行,删除多余的视频文件
     */
//    @Scheduled(cron = "0 0 2 * * ? ")
    @Scheduled(cron = "0/30 * * * * ? ")
    public void deleteVideoFile() {
        System.out.println("删除多余的上传文件啦");
        //删除本地多余文件
        uploadService.delete();
//将应用文件部分的文件id都存到同一个set集合中，然后再将upload中的id  set进去，若增加到集合中那么将这个的key在服务器中删除
        List<Video> videoList = videoFeign.getAllVideo();
        for (Video video : videoList) {
            if (!StringUtils.isEmpty(video.getUploadId()))
                redisUtil.sSet("uploadId", video.getUploadId());
            if (!StringUtils.isEmpty(video.getVideoCoverId()))
                redisUtil.sSet("uploadId", video.getVideoCoverId());
        }
        QueryWrapper<Upload> wrapper = new QueryWrapper<>();
        wrapper.select("fileId", "filePath");
        List<Upload> uploadList = uploadService.list(wrapper);
        for (Upload upload : uploadList) {
            long result = redisUtil.sSet("uploadId", upload.getFileId());
            if (result > 0) {
                uploadService.deleteFile(upload.getFilePath());
            }
        }
        System.out.println("删除完毕");
    }
//    @Scheduled(cron = "0/50 * * * * ? ")
//    public void deleteUserAvatarFile() {
//        System.out.println("删除user中的啦");
//        List<User> userAvatarIds = userFeign.getUserAvatarIds();
//        for (User user : userAvatarIds) {
//            if (!StringUtils.isEmpty(user.getUserAvatarId()))
//                redisUtil.sSet("uploadId", user.getUserAvatarId());
//        }
//        QueryWrapper<Upload> wrapper = new QueryWrapper<>();
//        wrapper.select("fileId", "filePath");
//        List<Upload> uploadList = uploadService.list(wrapper);
//        for (Upload upload : uploadList) {
//            long result = redisUtil.sSet("uploadId", upload.getFileId());
//            if (result > 0) {
//                uploadService.deleteFile(upload.getFilePath());
//            }
//        }
//        System.out.println("删除完毕");
//    }

    @RequestMapping("/fileSaveOrUpdate")
    public Upload saveOrUpdate(Upload upload) {
        return uploadService.saveOrUpdateUpload(upload);
    }


}

